package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.enums.RedisConstants;
import com.weishi.framework.redis.RedisCache;
import com.weishi.project.speedometer.vo.AverageSpeedVo;
import com.weishi.project.speedometer.vo.DevInfo;
import com.weishi.project.speedometer.vo.RealTimeDataVo;
import com.weishi.project.training.domain.ShipSpeedRecordLog;
import com.weishi.project.training.mapper.ShipSpeedRecordLogMapper;
import com.weishi.project.training.service.IShipSpeedRecordLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.annotation.Documented;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 船速仪历史数据Service业务层处理
 */
@Service
public class ShipSpeedRecordLogServiceImpl extends ServiceImpl<ShipSpeedRecordLogMapper, ShipSpeedRecordLog> implements IShipSpeedRecordLogService {

    @Autowired
    private ShipSpeedRecordLogMapper shipSpeedRecordLogMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询船速仪历史数据列表
     */
    @Override
    public List<ShipSpeedRecordLog> selectShipSpeedRecordLogList(ShipSpeedRecordLog shipSpeedRecordLog) {
        return shipSpeedRecordLogMapper.selectShipSpeedRecordLogList(shipSpeedRecordLog);
    }

    @Override
    public List<RealTimeDataVo> selectDataListByChannelId(int channelId) {
        ShipSpeedRecordLog params = new ShipSpeedRecordLog();
        params.setChannelId(channelId);
        List<ShipSpeedRecordLog> list = shipSpeedRecordLogMapper.selectShipSpeedRecordLogList(params);
        return ShipSpeedRecordLog.getRealTimeData(list);
    }

    @Override
    public List<RealTimeDataVo> selectDataListByChannelId(int channelId, int size) {
        Map<String,Object> params = new HashMap<>();
        params.put("channelId",channelId);
        params.put("pageNum",0);
        params.put("pageSize",size);
        List<ShipSpeedRecordLog> list = shipSpeedRecordLogMapper.selectShipSpeedRecordLogListByPage(params);
        Collections.reverse(list);
        return ShipSpeedRecordLog.getRealTimeData(list);
    }

    @Override
    public double qryCurrentSpeed(String shipSpeedSn,Integer channelId) {
        double currentSpeed = 0.0;
        try {
            Map<String,Object> params = new HashMap<>();
            params.put("shipSpeedSn",shipSpeedSn);
            if(channelId != null) params.put("channelId",channelId);
            params.put("pageNum",0);
            params.put("pageSize",20);
            List<ShipSpeedRecordLog> list = shipSpeedRecordLogMapper.selectShipSpeedRecordLogListByPage(params);
            Collections.reverse(list);
            if(list.size()>2){
                List<RealTimeDataVo> realTimeDataVos = ShipSpeedRecordLog.getRealTimeData(list);
                double distance = realTimeDataVos.get(realTimeDataVos.size()-1).getDistanceTotal();
                double time = realTimeDataVos.get(realTimeDataVos.size()-1).getTimeTotal();
                if(time!=0)currentSpeed = distance / time;
            }
            BigDecimal bg = new BigDecimal(currentSpeed);
            currentSpeed = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentSpeed;
    }

    @Override
    public double qryCurrentSpeed(String shipSpeedSn) {
        DevInfo devInfo = redisCache.getCacheObject(RedisConstants.Dev_Info_Prefix+shipSpeedSn);
        if(devInfo!=null){
            return devInfo.getSpeed();
        }
        return 0;
    }

}